package cn.tedu.submarine;

import java.util.Random;
import javax.swing.ImageIcon;
import java.awt.Graphics;
/** */
public abstract class SeaObject {
    public static final int  LIVE = 0;
    public static final int  DEAD = 1;
    protected int state = LIVE;//当前状态（默认存活)
    protected int width;   //宽
    protected int height;  //高
    protected int x;       //x坐标
    protected int y;       //y坐标
    protected int speed;   //移动速度
   public SeaObject (int width,int height){
        this.width=width;
        this.height=height;
        x=-width;
        Random rand=new Random();
        y=rand.nextInt(World.HEIGHT-height-150+1)+150;
        speed=rand.nextInt(3)+1;
    }
   public SeaObject(int width,int height,int x,int y,int speed){
        this.width=width;
        this.height=height;
        this.x=x;
        this.y=y;
        this.speed=speed;

    }

    public abstract void move();
   /**  获取对象的图片 */
    public abstract ImageIcon getImage();


    public boolean isLive(){
        return state==LIVE;
    }
    public boolean isDead(){
        return state==DEAD;
    }

    public void paintImage(Graphics g){
        if (this.isLive()){
            this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }

    public boolean isOutOfBounds(){
        return this.x>=World.WIDTH;
    }

    /** 检测碰撞   this:指一个对象 out   */
    public boolean isHit (SeaObject other){
        //假设this指潜艇，other指炸弹
        int x1 = this.x-other.width;  //潜艇的x-炸弹的宽
        int x2 = this.x+this.width;  //潜艇的x+潜艇的宽
        int y1 = this.y-other.height;  //潜艇的y-炸弹的高
        int y2 = this.y+this.height;  //潜艇的y+潜艇的高
        int x  = other.x; //炸弹的x
        int y  = other.y; //炸弹的y

        return  x>=x1 && x<=x2
                &&
                y>=y1 && y<=y2;
    }

    public void goDead(){
        state = DEAD;//将对象状态设置为DEAD死了的

    }








}
